Grokking Tree Coding Patterns for Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Course Overview
Basics of Tree
Introduction to Tree
Types of Tree
BST Traversal Techniques
BST Operations
Level Order Traversal Pattern (easy)
Introduction to Level Order Traversal Pattern
Reverse Level Order Traversal (easy)
Solution: Reverse Level Order Traversal
Find Largest Value in Each Tree Row (medium)
Solution: Find Largest Value in Each Tree Row
Maximum Width of Binary Tree (medium)
Solution: Maximum Width of Binary Tree
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Zigzag Traversal (medium)
Solution: Zigzag Traversal
Even Odd Tree (medium)
Solution: Even Odd Tree
N-ary Tree Level Order Traversal (hard)
Solution: N-ary Tree Level Order Traversal
Tree Depth Pattern (easy)
Introduction to Tree Depth Pattern
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Minimum Depth of a Binary Tree (easy)
Solution: Minimum Depth of a Binary Tree
Balanced Binary Tree (easy)
Solution: Balanced Binary Tree
Tree Diameter (easy)
Solution: Tree Diameter
Maximum Depth of N-ary Tree (easy)
Solution: Maximum Depth of N-ary Tree
Find maximum depth of subtree (medium)
Solution: Find maximum depth of subtree
Introduction to Root to Leaf Path Pattern
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Sum of Root To Leaf Binary Numbers (easy)
Solution: Sum of Root To Leaf Binary Numbers
Insufficient Nodes in Root to Leaf Paths (medium)
Solution: Insufficient Nodes in Root to Leaf Paths
Smallest String Starting From Leaf (medium)
Solution: Smallest String Starting From Leaf
Pseudo-Palindromic Paths in a Binary Tree (medium)
Solution: Pseudo-Palindromic Paths in a Binary Tree
All Paths for a Sum (medium)
Solution: All Paths for a Sum
Sum of Path Numbers (medium)
Solution: Sum of Path Numbers
Path With Given Sequence (medium)
Solution: Path With Given Sequence
Count Paths for a Sum (medium)
Solution: Count Paths for a Sum
Introduction to Leaf Processing Pattern
Sum of Left Leaves (easy)
Solution: Sum of Left Leaves
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Check if all leaves are at same level (medium)
Solution: Check if all leaves are at same level
Delete Leaves With a Given Value (medium)
Solution: Delete Leaves With a Given Value
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
Introduction to Tree View Pattern
Right View of a Binary Tree (medium)
Solution: Right View of a Binary Tree
Top View Of the Binary Tree (medium)
Solution: Top View Of the Binary Tree
Bottom View Of the Binary Tree (medium)
Solution: Bottom View Of the Binary Tree
Boundary of Binary Tree (medium)
Solution: Boundary of Binary Tree
Introduction to Comparison of Two Trees Pattern
Symmetric Tree (easy)
Solution: Symmetric Tree
Mirror Image Trees (medium)
Solution: Mirror Image Trees
Subtree of Another Tree (easy)
Solution: Subtree of Another Tree
Isomorphic Trees (medium)
Solution: Isomorphic Trees
Introduction to Serialize and Deserialize Tree Pattern
Serialize and Deserialize BST (medium)
Solution: Serialize and Deserialize BST
Verify Preorder Serialization of a Binary Tree (medium)
Solution: Verify Preorder Serialization of a Binary Tree
Create Binary Tree From Descriptions (medium)
Solution: Create Binary Tree From Descriptions
Serialize and Deserialize N-ary Tree (hard)
Solution: Serialize and Deserialize N-ary Tree
Solution: Maximum Depth (or Height) of Binary Tree

Problem Statement

Given a root node of the binary tree, return the depth (or height) of a binary tree.

The Depth of the binary tree refers to the number of nodes along the longest path from the root node to the farthest leaf node. If the tree is empty, the depth is 0.

Examples

Example 1

  • Input: root = [1, 2, 3, 4, 5]
Image
  • Expected Output: 3
  • Explanation: The longest path is 1->2->4 or 1->2->5 with 3 nodes.

Example 2

  • Input: root = [1, null, 2, null, 3]
Image
  • Expected Output: 3
  • Justification: There's only one path 1->2->3 with 3 nodes.

Example 3

  • Input: root = [1, 2, 3, 4, 7, null, null, null, null, null, 9]
Image
  • Expected Output: 4
  • Justification: The longest path is 1->2->7->9 with 4 nodes.

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -100 <= Node.val <= 100

Solution

What's the 'Depth'?

Picture a ladder. Each step is a level in our tree. The depth? It's just how many steps there are! If we have a 3-step ladder, our tree has a depth of 3.

To determine the deepest level of a binary tree, we'll use a depth-first search (DFS) approach. Begin at the root node and traverse down each branch, keeping track of the current depth. The depth increases by one each time we move to a child node. If a node is a leaf (no children), compare its depth with the current maximum depth and update if necessary. Recursively apply this process to each node's left and right children. This will cover all paths in the tree, allowing us to find and return the maximum depth encountered.

Step-by-step Algorithm

  1. Recursive Approach: The depth of a binary tree can be computed recursively. The maximum depth of a tree is the maximum of the depths of its left and right subtrees plus one (the root node).

  2. Base Condition: If the node is null, return 0. This ensures that we've reached the end of a branch or the tree is empty.

  3. Recursive Calculation: For each node, compute the depth of its left subtree and the depth of its right subtree. The depth of the current node is 1 + the maximum of these two depths.

  4. Result: For the root node, this recursive approach will provide the maximum depth of the entire tree.

Algorithm Walkthrough

Image
  1. Starting at the root node 1:

    • The method maxDepth(root) is called with the root node 1.
    • The root node is not null, so we proceed to calculate the depth of its left and right subtrees.
  2. Calculating the depth of the left subtree of node 1:

    • The method maxDepth(root.left) is called with the left child of node 1, which is node 2.
  3. Calculating the depth of the left subtree of node 2:

    • The method maxDepth(root.left.left) is called with the left child of node 2, which is node 4.
    • Node 4 is a leaf node (no left or right children).
    • The method maxDepth(root.left.left.left) is called with null (left child of 4), returning 0.
    • The method maxDepth(root.left.left.right) is called with null (right child of 4), returning 0.
    • The maximum depth from node 4 is 1 + max(0, 0) = 1.
  4. Calculating the depth of the right subtree of node 2:

    • The method maxDepth(root.left.right) is called with the right child of node 2, which is node 5.
    • Node 5 is a leaf node (no left or right children).
    • The maximum depth from node 5 is 1 + max(0, 0) = 1.
  5. Determining the depth of node 2:

    • Node 2 has a left depth of 1 (from node 4) and a right depth of 1 (from node 5).
    • The maximum depth from node 2 is 1 + max(1, 1) = 2.
  6. Calculating the depth of the right subtree of node 1:

    • The method maxDepth(root.right) is called with the right child of node 1, which is node 3.
    • Node 3 is a leaf node (no left or right children).
    • The maximum depth from node 3 is 1 + max(0, 0) = 1.
  7. Determining the depth of the root node 1:

    • Node 1 has a left depth of 2 (from node 2) and a right depth of 1 (from node 3).
    • The maximum depth of the tree rooted at node 1 is 1 + max(2, 1) = 3.

Final Output: The maximum depth of the tree [1, 2, 3, 4, 5] is 3.

Code

Here is the code for this algorithm:

Java

. . . .

Complexity Analysis

Time Complexity

  1. Recursive Calls:

    • The algorithm uses recursion to traverse each node in the binary tree exactly once. For each node, it makes a recursive call for the left child and the right child.
    • If the binary tree has n nodes, there will be n recursive calls in total.
    • Therefore, the time complexity for traversing the entire tree is .
  2. Calculating Maximum Depth:

    • For each node, the algorithm calculates the maximum of the left and right subtree depths, which is a constant time operation .

Combining these steps, the overall time complexity of the algorithm is .

Space Complexity

  1. Recursive Stack Space:

    • The depth of the recursion stack is determined by the height of the tree. In the worst case, if the tree is skewed (i.e., all nodes are in a single line), the height of the tree will be n, resulting in a space complexity of for the recursion stack.
    • In the best case (a balanced tree), the height of the tree will be , resulting in a space complexity of .
  2. Additional Space:

    • The algorithm does not use any additional space other than the recursion stack.
Maximum Depth (or Height) of Binary Tree (easy)
Minimum Depth of a Binary Tree (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity